package com.cloudptt.api.product.pjsua2;

import com.cloudptt.api.pjsip.api.ImsProfile;
import com.cloudptt.api.product.log.Log;
import com.mcptt.config.body.ResourceListsBody;
import com.mcptt.db.model.SessionInfo;

import org.pjsip.pjsua2.Buddy;
import org.pjsip.pjsua2.BuddyConfig;
import org.pjsip.pjsua2.BuddyInfo;
import org.pjsip.pjsua2.OnBuddyEvSubStateParam;
import org.pjsip.pjsua2.SipHeader;
import org.pjsip.pjsua2.SipHeaderVector;
import org.pjsip.pjsua2.SipMediaType;
import org.pjsip.pjsua2.SipMultipartPart;
import org.pjsip.pjsua2.SipTxOption;
import org.pjsip.pjsua2.pjsip_evsub_state;
import org.pjsip.pjsua2.pjsua_buddy_status;

public class MyBuddy extends Buddy
{
    public static String THIS_FILE = "MyBuddy";
    public BuddyConfig cfg;

    public MyBuddy(BuddyConfig config)
    {
        super();
        cfg = config;
        Log.d(THIS_FILE, "MyBuddy config = " + config);
    }

//    public String getStatusText()
//    {
//        BuddyInfo bi;
//
//        try {
//            bi = getInfo();
//        } catch (Exception e) {
//            return "?";
//        }
//
//        String status = "";
//        if (bi.getSubState() == pjsip_evsub_state.PJSIP_EVSUB_STATE_ACTIVE) {
//            if (bi.getPresStatus().getStatus() ==
//                    pjsua_buddy_status.PJSUA_BUDDY_STATUS_ONLINE)
//            {
//                status = bi.getPresStatus().getStatusText();
//                if (status == null || status.length()==0) {
//                    status = "Online";
//                }
//            } else if (bi.getPresStatus().getStatus() ==
//                    pjsua_buddy_status.PJSUA_BUDDY_STATUS_OFFLINE)
//            {
//                status = "Offline";
//            } else {
//                status = "Unknown";
//            }
//        }
//        return status;
//    }

    public SipHeaderVector getMsgDataSipHeaderVector(String msgType, String sendMcdataId)
    {
        SipHeaderVector vec = new SipHeaderVector();
        SipHeader header = new SipHeader();
        header.setHName("Accept-Contact");
        if (!SessionInfo.isFdMsg(msgType)) {
            header.setHValue("*;+g.3gpp.mcdata.sds;require;explicit");
        } else {
            header.setHValue("*;+g.3gpp.mcdata.fd;require;explicit");
        }
        vec.add(header);

        header = new SipHeader();
        header.setHName("Accept-Contact");
        if (!SessionInfo.isFdMsg(msgType)) {
            header.setHValue("*;+g.3gpp.icsi-ref=\"urn:urn-7:3gpp-service.ims.icsi.mcdata.sds\";require;explicit");
        } else {
            header.setHValue("*;+g.3gpp.icsi-ref=\"urn:urn-7:3gpp-service.ims.icsi.mcdata.fd\";require;explicit");
        }
        vec.add(header);

        header = new SipHeader();
        header.setHName("P-Preferred-Service");
        if (!SessionInfo.isFdMsg(msgType)) {
            header.setHValue("urn:urn-7:3gpp-service.ims.icsi.mcdata.sds");
        } else {
            header.setHValue("urn:urn-7:3gpp-service.ims.icsi.mcdata.fd");
        }
        vec.add(header);

        header = new SipHeader();
        header.setHName("P-Preferred-Identity");
        header.setHValue("<"+sendMcdataId+">");
        vec.add(header);

        return vec;
    }


//    @Override
//    public void onBuddyEvSubState(OnBuddyEvSubStateParam prm) {
//        super.onBuddyEvSubState(prm);
//        Log.debug(this.getClass(),"onBuddyEvSubState ");
//    }
//
//    @Override
//    public void onBuddyState()
//    {
////        MyApp.observer.notifyBuddyState(this);
//    }

    public String toString()
    {
        StringBuffer strBuf = new StringBuffer();
        strBuf.append("Buddy").append(";");
        strBuf.append("Valid").append("=").append(this.isValid()).append(";");
//        try {
//            strBuf.append("Info").append("=").append(this.getInfo()).append(";");
//        }
//        catch (Exception e)
//        {}
        return strBuf.toString();
    }

    public static SipMultipartPart getSipMultipartPart(String type, String subType,String content)
    {
        SipMultipartPart part = new SipMultipartPart();
        SipMediaType sipMediaType = new SipMediaType();
        sipMediaType.setType(type);
        sipMediaType.setSubType(subType);
        part.setContentType(sipMediaType);
        part.setBody(content);
        return part;
    }
}
